﻿//If the SDK namespace object is not defined, create it.
if (typeof (MCS) == "undefined")
{ MCS = {}; }
// Create Namespace container for functions in this library;
MCS.User_RibbonFunctions = {};

MCS.User_RibbonFunctions.FORM_TYPE_CREATE = 1;
MCS.User_RibbonFunctions.FORM_TYPE_UPDATE = 2;
MCS.User_RibbonFunctions.FORM_TYPE_READ_ONLY = 3;
MCS.User_RibbonFunctions.FORM_TYPE_DISABLED = 4;

MCS.User_RibbonFunctions.DeleteResourceConnections = function () {

    var deleteUserConnections = Xrm.Page.getAttribute("cvt_deleteuserconnections");

    var r = confirm("Please confirm that you would like to remove all connections to MTSA's, TSA's, and Resource Groups for this TMP User. Please Select Ok to continue or Cancel to abort.");
    if (r == true) {
        x = alert("All connections to MTSA's, TSA's, and Resource Groups for this TMP User are being removed. You may also choose to delete this TSS Resource by selecting the delete button in ribbon.")
        deleteUserConnections.setValue(true);
        Xrm.Page.data.entity.save();
    }
    else {
        x = "Aborted";
        deleteResourceConnections.setValue(false);
        Xrm.Page.data.entity.save();
    }   
}


MCS.User_RibbonFunctions.ReplaceResource = function () {

    var updateUserConnections = Xrm.Page.getAttribute("cvt_updateuserconnections");
    var EntityName = Xrm.Page.data.entity.getEntityName();
    var EntityId = Xrm.Page.data.entity.getId();

    var r = confirm("Please confirm that you would like to replace this TMP User on all MTSA's, TSA's, and Resource Groups. Please Select Ok to continue or Cancel to abort.");
    if (r == true) {
        x =
        MCS.User_RibbonFunctions.openDialogProcess("F18CA404-A532-4DA9-941F-866F2C9FA388", EntityName, EntityId);
    }
    else {
        x = "Aborted";
        updateUserConnections.setValue(false);
        Xrm.Page.data.entity.save();
    }
}

MCS.User_RibbonFunctions.openDialogProcess = function (dialogId, EntityName, objectId) {
    var url = Xrm.Page.context.getClientUrl() +
      "/cs/dialog/rundialog.aspx?DialogId=" +
      dialogId + "&EntityName=" +
      EntityName + "&ObjectId=" +
      objectId;
    var width = 400;
    var height = 400;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    window.open(url, '', 'location=0,menubar=1,resizable=0,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + '');
}
